# Maintainer: J. Peter Mugaas <jpmugaas@suddenlink.net>

_realname=directxmesh
_rn=DirectXMesh
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
# Version from CMakeLists.txt
pkgver=1.6.8
_tag=mar2025
pkgrel=1
pkgdesc="DirectXMesh geometry processing library (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url='http://go.microsoft.com/fwlink/?LinkID=324981'
msys2_repository_url="https://github.com/microsoft/DirectXMesh"
license=('spdx:MIT')
depends=("${MINGW_PACKAGE_PREFIX}-directxmath"
         "${MINGW_PACKAGE_PREFIX}-directx-headers"
         "${MINGW_PACKAGE_PREFIX}-gcc-libs")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja")
source=("https://github.com/microsoft/DirectXMesh/archive/${_tag}/${_realname}-${_tag}.tar.gz")
sha256sums=('9b4cb3cf331dd517a42393e8f408f06c0758ddf92da192d341b827469fb02ca2')

build() {
  cd "${srcdir}/${_realname}-${_tag}"

  declare -a extra_config
  if check_option "debug" "n"; then
    extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  if [ "${CARCH}" == "x86_64" ]; then
    extra_config+=("-DDIRECTX_ARCH=x64")
  fi
  if [ "${CARCH}" == "aarch64" ]; then
    extra_config+=("-DDIRECTX_ARCH=arm64")
  fi

  mkdir -p "${srcdir}/build-${MSYSTEM}-static" && cd "${srcdir}/build-${MSYSTEM}-static"

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    "${MINGW_PREFIX}"/bin/cmake.exe \
      -GNinja \
      -DCMAKE_INSTALL_PREFIX="${MINGW_PREFIX}" \
      -DBUILD_SHARED_LIBS=OFF \
      "${extra_config[@]}" \
      -DBUILD_TOOLS=ON \
      -DBUILD_DX12=ON \
      ../${_realname}-${_tag}

  "${MINGW_PREFIX}"/bin/cmake.exe --build .

  mkdir -p "${srcdir}/build-${MSYSTEM}-shared" && cd "${srcdir}/build-${MSYSTEM}-shared"

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    "${MINGW_PREFIX}"/bin/cmake.exe \
      -GNinja \
      -DCMAKE_INSTALL_PREFIX="${MINGW_PREFIX}" \
      -DBUILD_SHARED_LIBS=ON \
      "${extra_config[@]}" \
      -DBUILD_TOOLS=ON \
      -DBUILD_DX12=ON \
      ../${_realname}-${_tag}

  "${MINGW_PREFIX}"/bin/cmake.exe --build .
}

package() {
  cd "${srcdir}/build-${MSYSTEM}-static"
  DESTDIR="${pkgdir}" "${MINGW_PREFIX}"/bin/cmake.exe --install .

  cd "${srcdir}/build-${MSYSTEM}-shared"
  DESTDIR="${pkgdir}" "${MINGW_PREFIX}"/bin/cmake.exe --install .

  install -Dm644 "${srcdir}"/${_realname}-${_tag}/LICENSE \
    "${pkgdir}"${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE
}
